package com.anjoyo.xgh.musicshare.adapter;

import java.util.ArrayList;

import net.tsz.afinal.FinalBitmap;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.sharesdk.framework.compro.m;

import com.anjoyo.xgh.musicshare.R;
import com.anjoyo.xgh.musicshare.api.AsynImageload;
import com.anjoyo.xgh.musicshare.entry.ShareBean;
import com.anjoyo.xgh.musicshare.util.Constant;
import com.anjoyo.xgh.musicshare.util.PullToRefreshListView;
import com.anjoyo.xgh.musicshare.util.StringUtil;

public class ShareAdapter extends BaseAdapter {
	ArrayList<ShareBean> shareBeans;
	LayoutInflater inflater;
	PullToRefreshListView listView;
	AsynImageload asynImageload;
	FinalBitmap finalBitmap;
	Handler handler;

	public ShareAdapter(Context con, ArrayList<ShareBean> shareBeans,
			PullToRefreshListView listView, Handler handler) {
		this.shareBeans = shareBeans;
		this.listView = listView;
		inflater = LayoutInflater.from(con);
		asynImageload = new AsynImageload();
		finalBitmap = FinalBitmap.create(con);
		finalBitmap.configLoadfailImage(R.drawable.v5_0_1_photo_default_img);
		finalBitmap.configMemoryCacheSize(15);
		finalBitmap.configDiskCachePath(Constant.NOCALEAR_CACHE);
		finalBitmap.configDiskCacheSize(100);
		// finalBitmap.configDisplayer(displayer);
		this.handler = handler;
	}

	int i;

	@Override
	public int getCount() {
		return shareBeans.size();
	}

	@Override
	public Object getItem(int arg0) {
		return shareBeans.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(final int arg0, View convertView, ViewGroup arg2) {
		i++;
		final ViewHolder h;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.share_item, null);
			h = new ViewHolder();
			h.ivhead = (ImageView) convertView.findViewById(R.id.iv_head);
			h.ivshare = (ImageView) convertView.findViewById(R.id.imageView1);
			h.tvUname = (TextView) convertView.findViewById(R.id.tv_username);
			h.tvDate = (TextView) convertView.findViewById(R.id.tv_time);
			h.tvmsg = (TextView) convertView.findViewById(R.id.textView1);
			h.tvaddress = (TextView) convertView.findViewById(R.id.textView2);
			h.rLayout = (RelativeLayout) convertView
					.findViewById(R.id.relativeLayout1);
			h.tvplayaudio = (TextView) convertView
					.findViewById(R.id.tv_playaudio);
			h.tvplaymusic = (TextView) convertView.findViewById(R.id.tv_play);
			h.tvlike = (TextView) convertView.findViewById(R.id.tv_like);
			h.tvtxtlike = (TextView) convertView.findViewById(R.id.tv_txtlike);
			convertView.setTag(h);
		} else {
			h = (ViewHolder) convertView.getTag();
		}
		h.tvUname.setText(shareBeans.get(arg0).getUser().getNickname());
		h.tvDate.setText(StringUtil.dateChange(shareBeans.get(arg0).getTime()));
		h.tvmsg.setText(shareBeans.get(arg0).getContent());
		h.tvaddress.setText(shareBeans.get(arg0).getAddress());
		h.tvlike.setText(shareBeans.get(arg0).getLikecount() + "");
		h.tvtxtlike.setText(shareBeans.get(arg0).getLikecount() + "");
		h.tvplayaudio.setText("录音  "+shareBeans.get(arg0).getAudiotime()+"s");
		
		// 1
		String headurl = shareBeans.get(arg0).getUser().getSmall_icon();
		if (headurl.startsWith("http:")) {
			finalBitmap.display(h.ivhead, headurl);
		} else {
			finalBitmap.display(h.ivhead, Constant.SERVER_IP + headurl);
		}
		// h.ivhead.setTag(headurl + i);
		// System.out.println(headurl+i);
		// Bitmap bitmap = asynImageload.loadImageview(headurl, new
		// ImageCallback() {
		//
		// @Override
		// public void loadImage(Bitmap bitmap, String url) {
		// ImageView img = (ImageView) listView.findViewWithTag(url + i--);
		// System.out.println(url);
		// if (img != null) {
		// img.setImageBitmap(bitmap);
		// }
		// }
		// });
		// if (bitmap != null) {
		// h.ivhead.setImageBitmap(bitmap);
		// } else {
		// h.ivhead.setImageResource(R.drawable.v5_0_1_profile_headphoto);
		// }
		// 2
		String shareimgurl = shareBeans.get(arg0).getImg_lager();
		h.rLayout.setVisibility(View.VISIBLE);
		h.tvtxtlike.setVisibility(View.GONE);
		if (shareimgurl != null && !shareimgurl.equals("")) {
			finalBitmap.display(h.ivshare, Constant.SERVER_IP + shareimgurl);
		} else {
			h.rLayout.setVisibility(View.GONE);
			h.tvtxtlike.setVisibility(View.VISIBLE);
		}
		if (shareBeans.get(arg0).isIslike()) {
			h.tvlike.setBackgroundResource(R.drawable.feed_liked);
			h.tvtxtlike.setBackgroundResource(R.drawable.feed_liked);
		}else {
			h.tvlike.setBackgroundResource(R.drawable.feed_unlike);
			h.tvtxtlike.setBackgroundResource(R.drawable.feed_unlike);
		}
		// h.ivshare.setTag(shareimgurl);
		// Bitmap bitmap2 = asynImageload.loadImageview(shareimgurl,
		// new ImageCallback() {
		//
		// @Override
		// public void loadImage(Bitmap bitmap, String url) {
		// ImageView img = (ImageView) listView
		// .findViewWithTag(url);
		// if (img != null) {
		// img.setImageBitmap(bitmap);
		// }
		// }
		// });
		// if (bitmap2 != null) {
		// h.ivshare.setImageBitmap(bitmap2);
		// } else {
		// h.ivshare.setImageResource(R.drawable.zz);
		// }
		h.tvplayaudio.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Message message = new Message();
				message.what = Constant.MSG_SHARE_BACK;
				message.arg1 = 1;
				message.obj = getItem(arg0);
				handler.sendMessage(message);
			}
		});
		h.tvplaymusic.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Message message = new Message();
				message.what = Constant.MSG_SHARE_BACK;
				message.arg1 = 2;
				message.obj = getItem(arg0);
				handler.sendMessage(message);
			}
		});
		/*------------------------‘赞’的监听事件-----------------------------*/
		h.tvlike.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (!shareBeans.get(arg0).isIslike()){
					Message message = new Message();
					message.what = Constant.MSG_SHARE_BACK;
					message.arg1 = 3;
					message.obj = getItem(arg0);
					handler.sendMessage(message);
					h.tvlike.setBackgroundResource(R.drawable.feed_liked);
					h.tvlike.setText((((ShareBean) getItem(arg0)).getLikecount() + 1)
							+ "");
				}
			}
		});
		h.tvtxtlike.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (!shareBeans.get(arg0).isIslike()){
					Message message = new Message();
					message.what = Constant.MSG_SHARE_BACK;
					message.arg1 = 3;
					message.obj = getItem(arg0);
					handler.sendMessage(message);
					h.tvtxtlike.setBackgroundResource(R.drawable.feed_liked);
					h.tvtxtlike.setText((((ShareBean) getItem(arg0)).getLikecount() + 1)
							+ "");
				}
			}
		});
		/*------------------------‘赞’的监听事件-----------------------------*/
		return convertView;
	}

	class ViewHolder {
		ImageView ivhead, ivshare;
		TextView tvUname, tvDate, tvmsg, tvaddress, tvplayaudio, tvplaymusic,
				tvlike,tvtxtlike;
		RelativeLayout rLayout;
	}

}